<!--
 * @Author: maqaing
 * @Date: 2021-10-22 14:14:51
 * @LastEditors: [wcy]
 * @LastEditTime: 2021-11-02 18:10:02
 * @Description: 
-->
<template>
  <div>
    <!-- 按钮 -->
    <div class="btns">
   
      <el-form :inline="true" :model="queryParams" class="demo-form-inline" size="small">
        <el-button @click="toAddHandler" type="primary" size="small" style="margin-right:10px"
          >添加</el-button
        >
        <el-form-item>
          <el-input
            v-model="queryParams.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryParams.status" placeholder="请选择状态">
            <el-option label="禁用" value="禁用"></el-option>
            <el-option label="正常" value="正常"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="pageQueryCustomer">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <el-table :data="tableData.list" style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="realname" label="姓名"> </el-table-column>
      <el-table-column prop="telephone" label="手机号"> </el-table-column>
      <el-table-column label="性别">
        <template v-slot="scope">
          <div v-if="scope.row.gender">
            <span v-if="scope.row.gender == 'female'">女</span>
            <span v-else>男</span>
          </div>

        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="180" >
        <template slot-scope="scope">
          <div v-if="scope.row.status">
            <el-tag v-if="scope.row.status=='正常'" type="success" size="small">正常</el-tag>
            <el-tag v-else type="danger" size="small">禁用</el-tag>
          </div>
        </template>
        
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="toEdit(scope.row)"
            size="small"
            style="color: #51bca1"
            >编辑</el-button
          >
          <el-button
            type="text"
            @click="todelete(scope.row.id)"
            size="small"
            style="color: #e86e7d"
            >删除</el-button
          >
          <el-button
            type="text"
            @click="toMore(scope.row.id)"
            size="small"
            style="color: #4679ad"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pageQuery">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tableData.page"
        :page-sizes="[6, 9]"
        :page-size="tableData.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="tableData.total"
      >
      </el-pagination>
    </div>
    <!-- 模态框 -->
    <el-dialog :title="formTitle" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="ruleForm.realname"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="telephone">
          <el-input v-model="ruleForm.telephone"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="ruleForm.gender">
            <el-radio label="male">男</el-radio>
            <el-radio label="female">女</el-radio>
          </el-radio-group>
        </el-form-item>
         <el-form-item label="状态">
          <el-radio-group v-model="ruleForm.status">
            <el-radio label="正常">正常</el-radio>
            <el-radio label="禁用">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="toSaveHeadler">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "../../utils/request";
import qs from "qs";
export default {
  data() {
    return {
      queryParams: {
        page: 1,
        pageSize: 6,
        roleId: "6",
      },
      tableData: {},
      dialogFormVisible: false,
      ruleForm: {},
      formTitle: "",
      radio: "",
      allId: "",

      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        realname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        telephone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    pageQueryCustomer() {
      request
        .get("/baseUser/pageQuery", {
          params: this.queryParams,
        })
        .then((res) => (this.tableData = res.data));
    },
    handleSizeChange(pageSize) {
      this.queryParams.pageSize = pageSize;
      this.pageQueryCustomer();
    },
    handleCurrentChange(page) {
      this.queryParams.page = page;
      this.pageQueryCustomer();
    },
    // 按钮事件
    toAddHandler() {
      this.dialogFormVisible = true;
      this.ruleForm = {};
      this.formTitle = "添加客户信息";
    },
    // 模态框确定按钮
    toSaveHeadler() {
      // 点击编辑
      if (this.allId) {
        request
          .post("/baseUser/saveOrUpdate", qs.stringify(this.ruleForm))
          .then(() => {
            this.pageQueryCustomer();
            this.dialogFormVisible = false;
            this.$message.success("修改成功");
          });
        this.allId = "";
      }
      // 点击添加
      else {
        let params = {
          ...this.ruleForm,
          roleId: "6",
        };
        request
          .post("/baseUser/addUserWidthRole", qs.stringify(params))
          .then(() => {
            // 数据重载
            this.pageQueryCustomer();
            this.dialogFormVisible = false;
            this.$message.success("添加成功");
          });
      }
    },
    //编辑
    toEdit(row) {
      this.allId = row.id;
      this.dialogFormVisible = true;
      this.formTitle = "修改客户信息";
      this.ruleForm = { ...row };
    },
    // 删除
    todelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          request.get("/baseUser/deleteById", { params: { id } }).then(() => {
            this.pageQueryCustomer();
            this.$message.success("刪除成功");
          });
        })
        .catch(() => {
          this.$message.success("取消刪除");
        });
    },
    // 详情
    toMore(id){
      this.$router.push({
        path:'/more',
        query:{
         id
        }
        
      })
    }
  },

  created() {
    this.pageQueryCustomer();
  },
};
</script>

<style scoped>
.pageQuery {
  position: fixed;
  right: 50px;
}
</style>